<?php

/***************************************************************\
* FILE:		sys/pages/download.inc
* SECTION:	pages > items
* FUNCTION:	display a download
* USES:	
* INCLUDES:	/sys/itemfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/itemfunctions.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	item_page_inc_check_access();
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	switch($action) {
		default:
			item_page_inc_execute_action($action, 'download');
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE;
	$CONTENT = array();

	$where = "ITEM.item_id = {$PAGE['id']} AND \n\t";
	$where .= "ITEM.item_id = DOWNLOAD.item_id AND \n\t";
	$where .= "ITEM.author = M1.user_id AND \n\t";
	$where .= "ITEM.editor = M2.user_id";

	$sql = db_query_select(array(array('*'),array('*'),array(),"M1.nick AS nick_author, M2.nick AS nick_editor"), array('ITEM','DOWNLOAD','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), $where);
	$sqlresult = db_do_query($sql);

	if (db_num_rows($sqlresult) !=1)
		errorpage(404);

	$CONTENT['download'] = db_fetch_array($sqlresult);

	if ($CONTENT['download']['deleted'])
		errorpage(404);

	$size = filesize("down/" . $CONTENT['download']['filename']);
	if ($size > 800000) { 
		$size = substr(($size / (1024 * 1024)), 0, 4);
		if (substr($size,-1,1)==".")
			$size = substr($size,0,3);
		$size .= " Mb";
	} else if ($size > 1000) {
		$size = substr(($size/1024),0,4);
		if (substr($size,-1,1)==".")
			$size = substr($size,0,3);
		$size .= " kb"; 
	}
	$CONTENT['download']['size'] = $size;
	
	$CONTENT['title'] = $CONTENT['download']['title'];
	$CONTENT['owner'] = $CONTENT['download']['author'];
		
	$PAGE['content'] = $CONTENT;
	
	item_page_inc_execute('download');
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	item_page_inc_dress_before();
	print_download($PAGE['content']['download']);
	item_page_inc_dress_after();
}

?>